// 缺陷记录
import store from '../index'
import useConfirm from '@/hooks/useConfirm'
import { InspectResultkModel, InspectionDefectRecordModel } from '@/models'
const inspectResultkModel = new InspectResultkModel()
const inspectionDefectRecordModel = new InspectionDefectRecordModel()

const getDefaultState = () => {
    return {
        detailInfo: {}
    }
}

const state = getDefaultState()

const getters = {}

const mutations = {
    setDetailInfo(state, detailInfo) {
        state.detailInfo = detailInfo
    }
}

const actions = {
    async add({ commit }, data) {
        const res = await useConfirm({
            text: '是否确定保存表单',
            model: inspectResultkModel,
            modelAction: 'add',
            data
        })
        return res
    },
    async delete({ commit }, id) {
        const res = await useConfirm({
            text: '是否确定删除表单',
            model: inspectResultkModel,
            modelAction: 'delete',
            data: { id }
        })
        return res
    },
    async detail({ commit }, id) {
        try {
            store.commit('openLoading')
            const res = await inspectResultkModel.detail({ id })

            commit('setDetailInfo', inspectResultkModel.transformNull(res))
            return res
        } finally {
            store.commit('closeLoading')
        }
    },
    // 创建缺陷记录
    async submitFlawRecord({ commit }, data) {
        try {
            store.commit('openBtnLoading')
            // const res = await inspectResultkModel.submitFlawRecord(data)
            const res = await inspectionDefectRecordModel.saveOrUpdate(data)
            return res
        } finally {
            store.commit('closeBtnLoading')
        }
    }
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}
